function myDate(defaultDate) {
    this._mydate = new Date();
    this.init = this.refresh;
    this.init();
    if ( defaultDate ? true : false) {

        var t = defaultDate.split("-"), year = t[0], month = t[1], date = t[2];
        this._mydate = new Date();
        this._mydate.setFullYear(year);
        this._mydate.setDate(1);
        this._mydate.setMonth(month - 1);
        this._mydate.setDate(date);
        this.refresh();
        /*
         this.setYear(year);
         this.setDate(1);
         this.setMonth(month);
         this.setDate(date);
         */
    }
}

/*
 * Month Name
 */
myDate.MonName = {
    1 : "January",
    2 : "February",
    3 : "March",
    4 : "April",
    5 : "May",
    6 : "June",
    7 : "July",
    8 : "August",
    9 : "September",
    10 : "October",
    11 : "November",
    12 : "Descember"
};
myDate.MonNameShort = {
    1 : "Jan",
    2 : "Feb",
    3 : "Mar",
    4 : "Apr",
    5 : "May",
    6 : "Jun",
    7 : "Jul",
    8 : "Aug",
    9 : "Sep",
    10 : "Oct",
    11 : "Nov",
    12 : "Dec"
};
/*
 * Week Name
 */
myDate.WeekName = {
    0 : "Sunday",
    1 : "Monday",
    2 : "Tuesday",
    3 : "Wednesday",
    4 : "Thursday",
    5 : "Friday",
    6 : "Saturday"
};
myDate.WeekNameShort = {
    0 : "Sun",
    1 : "Mon",
    2 : "Tue",
    3 : "Wed",
    4 : "Thu",
    5 : "Fri",
    6 : "Sat"
};
/*
 * DateRange Type
 */
myDate.Type = {
    DAILY : "D",
    MONTHLY : "M",
    WEEKLY : "W",
    QUARTERLY : "Q",
    YEARLY : "Y"
};
/*
 * Cookie Name
 */
myDate.CookieName = "pt_myDate_cookie_value";
/*
 * STARTYEAR and ENDYEAR
 */
myDate.STARTYEAR = 2009;
myDate.ENDYEAR = 2020;
/*
 * DateRangeTypeMap
 */
myDate.DateRangeTypeMap = {
    "D" : "220px",
    "W" : "360px",
    "M" : "240px",
    "Q" : "240px",
    "Y" : "260px"
};

/*
 *  myDate.cookieExpireTime 30days
 */
myDate.expireDays = 30;
myDate.cookieExpireTime = new Date();
myDate.cookieExpireTime.setTime((new Date()).getTime() + myDate.expireDays * 24 * 3600 * 1000);

/*
 * Refresh myDate
 */
myDate.prototype.refresh = function() {
    this.date = this.getDate();
    this.day = this.getDay();
    this.month = this.getMonth();
    this.year = this.getYear();
    this.week = this.getWeek();
    this.quarter = this.getQuarter();
};
/*
 * GetDate
 */
myDate.prototype.getDate = function() {
    return this._mydate.getDate();
};
/*
 * GetDay
 */
myDate.prototype.getDay = function() {
    return this._mydate.getDay();
};
/*
 * GetWeek
 */
myDate.prototype.getWeek = function() {
    var month = this.month;
    var date = this.date;
    var year = this._mydate.getFullYear();
    for (var i = this._mydate.getTime(); this._mydate.getDay() < 6 && this._mydate.getFullYear() == year; i = i + 24 * 60 * 60 * 1000) {
        if(this._mydate.getDay() != 6){
            this._mydate.setTime(i);
        }
        //this._mydate.getDay() != 6 ? this._mydate.setTime(i) : "";
    }
    var timeNow = this._mydate.getTime();
    //this._mydate.setYear(year);
    this._mydate.setMonth(0);
    this._mydate.setDate(1);
    var timePass = this._mydate.getTime();
    this._mydate.setYear(year);
    this._mydate.setMonth(month - 1);
    this._mydate.setDate(date);
    var dif = timeNow - timePass;
    return (Math.floor(dif / 1000 / 60 / 60 / 24 / 7) + 1);
};
/*
 * GetMonth
 */
myDate.prototype.getMonth = function() {
    return this._mydate.getMonth() + 1;
};
/*
 * GetMonthLastDay
 */
myDate.prototype.getMonthLastDay = function() {
    var _date = new myDate(this.toString()), month = _date._mydate.getMonth();
    while (_date._mydate.getMonth() == month) {
        _date.addDate(1);
    }
    _date.addDate(-1);
    return _date._mydate.getDate();
};
/*
 * GetQuarter
 */
myDate.prototype.getQuarter = function() {
    var flag = (parseInt((this.month - 1) / 3,0));
    var _quarter = "";
    switch(flag) {
        case 0:
            _quarter = 1;
            break;
        case 1:
            _quarter = 2;
            break;
        case 2:
            _quarter = 3;
            break;
        case 3:
            _quarter = 4;
            break;
        default:
            break;
    }
    return _quarter;

};
/*
 * GetYear
 */
myDate.prototype.getYear = function() {
    return this._mydate.getFullYear();
};
/*
 * GetYearLastDay
 */
myDate.prototype.getYearLastDay = function() {
    return "31";
};
/*
 * GetYearLastMonth
 */
myDate.prototype.getYearLastMonth = function() {
    return "12";
};
/*
 * SetDate
 */
myDate.prototype.setDate = function(date) {

    if (!date || date < 1 || date > 31) {
        MsgBox.show("Error Input");
        return;
    }
    this._mydate.setDate(date);
    this.refresh();
    return this;
};
/*
 * SetWeek
 */
myDate.prototype.setWeek = function(week) {
    if (!week || week < 1 || week > 53) {
        MsgBox.show("Error Input");
        return;
    }
    var i = 1;
    this._mydate.setMonth(0);
    do {
        this._mydate.setDate(i);
        i++;
    } while(this._mydate.getDay()<6);
    var timePass = this._mydate.getTime();
    var countweek = 1;
    this._mydate.setTime(timePass + ((week - 1) * 7) * 24 * 60 * 60 * 1000);
    this.refresh();
    return this;
};
/*
 * SetMonth
 */
myDate.prototype.setMonth = function(month) {
    if (!month || month < 1 || month > 12) {
        MsgBox.show("Error Input");
        return;
    }
    var tmp = new Date();
    tmp.setTime(this._mydate.getTime());
    tmp.setMonth(month - 1);
    if (tmp.getMonth() != (month - 1)) {
        for (var i = 30; i >= 28; i--) {
            tmp.setTime(this._mydate.getTime());
            tmp.setDate(i);
            tmp.setMonth(month - 1);
            if (tmp.getMonth() == month - 1) {
                break;
            }
        }
    }
    this._mydate.setTime(tmp.getTime());
    this.refresh();
    return this;
};
/*
 * SetQuarter
 */
myDate.prototype.setQuarter = function(quarter) {
    if (!quarter || quarter > 4 || quarter < 1) {
        MsgBox.show("Error Input");
        return;
    }
    switch(quarter) {
        case 1:
            this.setDate(1);
            this.setMonth(3);
            this.setDate(31);
            break;
        case 2:
            this.setDate(1);
            this.setDate(30);
            this.setMonth(6);
            break;
        case 3:
            this.setDate(1);
            this.setMonth(9);
            this.setDate(30);
            break;
        case 4:
            this.setDate(1);
            this.setMonth(12);
            this.setDate(31);
            break;
        default:
            break;
    }
};
/*
 * SetYear
 */
myDate.prototype.setYear = function(year) {
    //
    if (!year || year > 3000 || year < 1) {
        MsgBox.show("Error Input");
        return;
    }
    this._mydate.setFullYear(year);
    this.refresh();
    return this;
};
/*
 *  setTime Method
 */
myDate.prototype.setTime = function(time) {
    try {
        var timeArr = time.split("-"), year = timeArr[0], month = timeArr[1], date = timeArr[2];
        this.setYear(year);
        this.setMonth(month);
        this.setDate(date);
    } catch(err) {
        console.log(e);
        MsgBox.show(myDate.parseJSON(err));
    }
    return this;
};
/*
 *  toString Method
 */
myDate.prototype.toString = function() {
    var obj = {
        _year : this.year,
        _month : this.month,
        _date : this.date
    };
    //return obj._year + "-" + obj._month + "-" + obj._date;
    return obj._year + "-" + ((obj._month > 9) ? obj._month : ("0" + obj._month)) + "-" + ((obj._date > 9) ? obj._date : ("0" + obj._date));
};
/*
 * addDate Method
 */
myDate.prototype.addDate = function(date) {
    var dateTime = date * 24 * 60 * 60 * 1000;
    var timeNow = this._mydate.getTime();
    this._mydate.setTime(timeNow + dateTime);
    this.refresh();
    return this;
};
/*
 * addWeek Method
 */
myDate.prototype.addWeek = function(week) {
    var dateTime = week * 7;
    this.addDate(dateTime);
    return this;
};
/*
 * addMonth Method
 */
myDate.prototype.addMonth = function(month) {
    month = parseInt(month,0);
    var year = parseInt((month + this.month) / 12,0);
    var mon = parseInt((month + this.month) % 12,0);
    var date = parseInt((this.getDate()),0);
    this._mydate.setYear(this.year + year);
    this._mydate.setDate(1);
    this._mydate.setMonth(mon - 1);
    this.year = year;
    this.month = mon;
    this.date = 1;
    this.refresh();
    date = date <= this.getMonthLastDay() ? date : this.getMonthLastDay();
    this._mydate.setDate(date);
    return this;
};
/*
 * addQuarter Method
 */
myDate.prototype.addQuarter = function(quarter) {
    var month = quarter * 3;
    this.addMonth(month);
    return this;
};
/*
 * addYear Method
 */
myDate.prototype.addYear = function(year) {
    var month = year * 12;
    this.addMonth(month);
    return this;
};
/*
 * getTime Method
 */
myDate.prototype.getTime = function() {
    return this._mydate.getTime();
};
/*
 * Date campare
 */
myDate.compareDate = function(date1, date2) {
    var dateList1 = date1.split("-");
    var dateList2 = date2.split("-");
    for (var i = 0; i < dateList1.length; i++) {
        if (parseFloat(dateList1[i]) > parseFloat(dateList2[i])) {
            return 1;
        } else if (parseFloat(dateList1[i]) < parseFloat(dateList2[i])) {
            return -1;
        }
    }
    return 0;
};
//计算天数差的函数，通用
myDate.dateDiff = function(sDate1, sDate2) {//sDate1和sDate2是2006-12-18格式
    var oDate1, oDate2, iDays;
    if (sDate1 == "" || sDate2 == "") {
        return NaN;
    }
    oDate1 = new myDate(sDate1);
    oDate2 = new myDate(sDate2);
    iDays = Math.round(Math.abs(oDate1._mydate.getTime() - oDate2._mydate.getTime()) / 1000 / 60 / 60 / 24);
    //把相差的毫秒数转换为天数
    return iDays;
};

exports.myDate = myDate;